﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jQueryDotNetLib.Controls
{
    [ParseChildren(typeof(Script))]
    [ToolboxData("<{0}:ScriptManager runat=server />")]
    [Designer(typeof(ScriptManagerDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public class ScriptManager : Panel
    {
 
        protected override void OnInit(EventArgs e)
        {
            RegisterCSS();
            RegisterjQuery();
            base.OnInit(e);
        }
        private void RegisterCSS()
        {
            if (jQueryTheme != null && jQueryTheme != "")
            {
                string cssLiteral = string.Format("<link type=\"text/css\" href=\"{0}\" rel=\"Stylesheet\" />", ResolveUrl(jQueryTheme));                
                Page.Header.Controls.Add(new LiteralControl(cssLiteral));
            }
        }
        private void RegisterjQuery()
        {
            if (!Page.ClientScript.IsClientScriptIncludeRegistered("jQueryJS"))
                Page.ClientScript.RegisterClientScriptInclude("jQueryJS", ResolveUrl(jQueryJS));

            if (!Page.ClientScript.IsClientScriptIncludeRegistered("jQuery.noConflict") && (EnableNoConflict))
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "jQuery.noConflict", "jQuery.noConflict();", true);

            if (!Page.ClientScript.IsClientScriptIncludeRegistered("jQueryUI")) 
                Page.ClientScript.RegisterClientScriptInclude("jQueryUI", ResolveUrl(jQueryUI));

            //jQueryDotNetLib.css.jqzoom.css
            foreach (Control c in this.Controls)
            {
                Script s = (c as Script);
                if (s != null)
                {
                    if (!Page.ClientScript.IsClientScriptIncludeRegistered(s.name))
                    {
                        
                        if (s.LocalResource)
                        {
                            System.Reflection.Assembly asm = System.Reflection.Assembly.Load(s.ResourceAssemblyName);
                            if (asm != null)
                            {
                                Type t = asm.GetType(s.ResourceTypeName);
                                string webRes = Page.ClientScript.GetWebResourceUrl(t, s.reference);
                                Page.ClientScript.RegisterClientScriptInclude(s.name, ResolveUrl(webRes));
                            }
                        } 
                        else
                            Page.ClientScript.RegisterClientScriptInclude(s.name, ResolveUrl(s.reference));
                    }
                }
            }
        }
        
        private const string ThemeCSSFile = "jquery-ui-1.7.1.custom.css";

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string jQueryTheme
        {
            get
            {
                if (ViewState["css"] == null)
                    return string.Format("{0}/{1}/{2}",
                        System.Configuration.ConfigurationManager.AppSettings["jQuery.ThemeFolder"],
                        System.Configuration.ConfigurationManager.AppSettings["jQuery.Theme"],
                        ThemeCSSFile
                        );
                return (string)ViewState["css"];
            }

            set
            {
                ViewState["css"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool EnableNoConflict
        {
            get
            {
                if (ViewState["EnableNoConflict"] == null) return false;
                return (bool)ViewState["EnableNoConflict"];
            }

            set
            {
                ViewState["EnableNoConflict"] = value;
            }
        }
        #region Statics
        static string jQueryJSFile = null;
        public string jQueryJS
        {
            get
            {
                if (jQueryJSFile != null) return jQueryJSFile;
                jQueryJSFile = Page.ClientScript.GetWebResourceUrl(this.GetType(), "jQueryDotNetLib.js.jquery-1.3.2.min.js");
                return jQueryJSFile;
            }
        }
        static string jQueryUIFile = null;
        public string jQueryUI
        {
            get
            {
                if (jQueryUIFile != null) return jQueryUIFile;
                jQueryUIFile = Page.ClientScript.GetWebResourceUrl(this.GetType(), "jQueryDotNetLib.js.jquery-ui-1.7.1.custom.min.js");
                return jQueryUIFile;
            }
        }
        #endregion

        protected override void Render(HtmlTextWriter writer)
        {                        
        }
    }
    public class ScriptManagerDesigner : System.Web.UI.Design.ControlDesigner
    {
        public override string GetDesignTimeHtml()
        {
            return "[jQuery ScriptManager]";
        }
    }
}